//
//  SelectCarController.m
//  DingDongXiChe
//
//  Created by wtjr on 2018/1/11.
//  Copyright © 2018年 xieyingze. All rights reserved.
//

#import "SelectCarController.h"
#import "CarBrandSectionView.h"
#import "CarTableCell.h"
#import "CarViewModel.h"
#import "CarBrandSectionView.h"

@interface SelectCarController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) CarViewModel *viewModel;

@end

@implementation SelectCarController

#pragma mark - ********************生命周期********************

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - ********************配置信息********************

- (void)configuration {
    self.navTitle = @"汽车型号选择";
}

- (void)initSubViews {
    [self.view addSubview:self.tableView];
}

- (void)autoLayoutForSubViews {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0).insets(UIEdgeInsetsMake(64, 0, 0, 0));
    }];
}

#pragma mark - ********************基础方法********************


#pragma mark - ********************代理方法********************

#pragma mark - UITableViewDataSource
//返回组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.viewModel.cars.count;
}

//返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    CarBrandModel *model = self.viewModel.cars[section];
    return model.isShow ?  model.cars.count : 0;
}

//返回Cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CarBrandModel *carBrand = self.viewModel.cars[indexPath.section];
    CardModel *model = carBrand.cars[indexPath.row];
    CarTableCell *cell = [CarTableCell cellWithTableView:tableView indexPath:indexPath];
    cell.model = model;
    cell.cellLineType = indexPath.row == carBrand.cars.count - 1 ? kCellLineTypeAllLine : kCellLineTypeNormal;
    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *sectionView = [UIView new];
    self.viewModel.section = section;
    CarBrandSectionView *carBrandView = [CarBrandSectionView new];
    carBrandView.viewModel = self.viewModel;
    carBrandView.section = section;
    [sectionView addSubview:carBrandView];
    [carBrandView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(@0);
    }];
    WeakSelf;
    carBrandView.clickCompeted = ^(NSInteger section) {
        [weakself updateTableView:section];
    };
    return sectionView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 50 * kUI_Percent;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    CarBrandModel *carBrand = self.viewModel.cars[indexPath.section];
    CardModel *model = carBrand.cars[indexPath.row];
    if (self.selectCompleted) {
        self.selectCompleted([NSString stringWithFormat:@"%@.%@",carBrand.brandName,model.name]);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - ********************响应事件********************

- (void)updateTableView:(NSInteger)section {
    CarBrandModel *model = self.viewModel.cars[section];
    model.isShow = !model.isShow;
    [self.tableView reloadData];
}

#pragma mark - ********************懒加载*********************

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.backgroundColor = kCOLOR_tableView;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 44 * kUI_Percent;
    }
    return _tableView;
}

- (CarViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [CarViewModel new];
    }
    return _viewModel;
}
@end
